﻿using StandardManager.DB;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace Jixiao
{
    public partial class FormNewTask : Form
    {
        public TaskItem? EditItem { get; internal set; }
        public string UserName { get; internal set; }
        public bool HasRichangWork { get; internal set; }
        public bool HasXieTongWork { get; internal set; }

        public FormNewTask()
        {
            InitializeComponent();
        }

        private void FormNewTask_Load(object sender, EventArgs e)
        {
            this.textBoxMyName.Text = UserName;
            this.textBoxAssignName.Text = "陈彪";

            ConnectionDatabase_S2 connectionDatabase_S2 = ConnectionDatabase_S2.BuildSqliteConnection(Setting.DBName);

            string sql = null;
            DataTable dataTable = null;

            if (EditItem == null)
            {
                sql = "SELECT MAX(rowId) FROM TaskList";
                dataTable = connectionDatabase_S2.SelectData(sql);
                if (dataTable.Rows.Count > 0 && dataTable.Rows[0][0] != DBNull.Value)
                {
                    int maxRowId = Convert.ToInt32(dataTable.Rows[0][0]);
                    textBoxRowID.Text = (maxRowId + 1).ToString();
                }
                else
                {
                    textBoxRowID.Text = "1";
                }
            }


            sql = "Select distinct(ProjectName) from TaskList";
            dataTable = connectionDatabase_S2.SelectData(sql);
            if (HasRichangWork == false)
                comboBox1.Items.Add("日常工作");
            if (HasXieTongWork == false)
                comboBox1.Items.Add("协同工作");
            if (dataTable.Rows.Count > 0 && dataTable.Rows[0][0] != DBNull.Value)
            {
                for (var i = 0; i < dataTable.Rows.Count; i++)
                {
                    if (dataTable.Rows[i][0].Equals("日常工作"))
                    {
                        continue;
                    }
                    if (dataTable.Rows[i][0].Equals("协同工作"))
                    {
                        continue;
                    }
                    this.comboBox1.Items.Add(dataTable.Rows[i][0]);
                }
                    
            }

            if (EditItem != null)
            {
                textBoxRowID.Text = EditItem.RowNumber.ToString();
                comboBox1.Text = EditItem.ProjectName;
                textBoxTaskName.Text = EditItem.TaskName;
                textBoxAssignName.Text = EditItem.AssignName;
                textBoxMyName.Text = EditItem.MyName;
                this.labelUUID.Text = EditItem.UUID;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ConnectionDatabase_S2 connectionDatabase_S2 = ConnectionDatabase_S2.BuildSqliteConnection(Setting.DBName);

            if (EditItem == null)
            {
                TaskItem item = new TaskItem()
                {
                    RowNumber = int.Parse(textBoxRowID.Text),
                    ProjectName = comboBox1.Text,
                    TaskName = textBoxTaskName.Text,
                    AssignName = textBoxAssignName.Text,
                    StartTime = DateTime.Now,
                    MyName = textBoxMyName.Text
                };

                connectionDatabase_S2.InsertData(item.CreateInsertSql(), null, null);
            }
            else
            {
                EditItem.ProjectName = comboBox1.Text;
                EditItem.TaskName = textBoxTaskName.Text;
                EditItem.AssignName = textBoxAssignName.Text;
                EditItem.MyName = textBoxMyName.Text;

                connectionDatabase_S2.ExecuteSQL(EditItem.BuildUpdateSql());
            }

            MessageBox.Show("OK");
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        
    }
}
